<script>
    // 账户模态表单页
    Vue.component('report-export', {
        template: `
      <el-dialog :visible.sync="dialogShow" width="800px" @close="closeModal">
        <h3 slot="title">
            <span>导出</span>
        </h3>
        <el-form ref="form"  label-width="180px">
            <el-form-item label="请选择报表" prop="memo">
                <el-select  placeholder="导出类型" v-model="export_type"   filterable default-first-option>
                <el-option
                v-for="item in exportTypes"
                :label="item.name"
                :value="item.index">
                </el-option>
            </el-select>
           </el-form-item>
            <el-form-item label="请选择导出形式" >
                                           <el-row>

                                                <el-radio   :label="0" v-model="type">明细</el-radio>
                                                <el-radio  :label="1" v-model="type">列表</el-radio>
                                            </el-row>
                                        </el-form-item>



        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="closeModal">取 消</el-button>
            <el-button type="primary" @click="handelExport">确 定</el-button>
        </div>
    </el-dialog>
    `,
        props: ['dialog-show','project_id','groupRecordType'],
        computed: {
            currentTallyId() {
                return this.$store.state.currentTallyId
            },
            currentTally () {
                return this.$store.state.currentTally
            },
            accountTags() {
                return this.$store.state.accountTags
            }
        },
        watch: {
            dialogShow:{
                handler (newVal) {},
                deep:true,
                immediate: true
            }
        },
        data:function(){
            return {
                export_type:1,
                tallyAccountForm: {},
                type:0,
                exportTypes:[
                    {'name':'收入流水','index':1},
                    {'name':'支出流水','index':2},
                    {'name':'应收/付流水','index':3},
                    {'name':'预收/付流水','index':4},
                ]
            }
        },
        methods: {
            closeModal(){
                this.$emit('modalClose')
            },
            handelExport(){
                let that = this;
                let params = {
                    'export_type':this.export_type,
                    'type':that.type,
                    'tally_id':that.currentTallyId,
                    'groupRecordType':this.groupRecordType
                };
                if(this.project_id != '' && this.project_id != null){
                    params['project_id'] = this.project_id;
                }
                axios.get('/api/tally-stat-report/export',{
                    method: 'get',
                    headers: {'token': loginToken},
                    params:params
                }).then(function(res) {
                    if(res.data.status === 'success'){
                        let filepath = res.data.data.filepath
                        let a = document.createElement('a');
                        a.target = "_blank";
                        a.href = filepath;
                        a.click();
                        console.log(res.data)
                    }else{
                        that.$message({
                            type: 'error',
                            message: '解析数据失败:'+res.data.error_message
                        })
                    }
                })
            }
        }
    })
</script>
<style>

</style>
